﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace dPet.Domain.Models
{
	using Hangerd;
	using Hangerd.Entity;

	public class ServiceItem : EntityBase
	{
		#region Public Properties

		/// <summary>
		/// 所属医院ID
		/// </summary>
		public string HospitalId { get; private set; }

		/// <summary>
		/// 服务类别
		/// </summary>
		public ServiceCategory Category { get; private set; }


		private string _name;

		/// <summary>
		/// 服务名称
		/// </summary>
		[RecordModify]
		public string Name
		{
			get { return _name; }
			set
			{
				if (string.IsNullOrWhiteSpace(value))
				{
					throw new HangerdException("服务名称不可为空");
				}

				_name = value.Trim();
			}
		}

		/// <summary>
		/// SKU属性集合
		/// </summary>
		public ICollection<ServiceSKUProperty> SKUProperties { get; private set; }

		/// <summary>
		/// SKU集合
		/// </summary>
		public ICollection<ServiceItemSKU> SKUList { get; private set; }

		/// <summary>
		/// 创建时间
		/// </summary>
		public DateTime CreateTime { get; private set; }

		#endregion

		#region Constructors

		private ServiceItem() { }

		public ServiceItem(Hospital hospital, string name)
		{
			this.HospitalId = hospital.Id;
			this.Name = name;
			this.SKUProperties = new List<ServiceSKUProperty>();
			this.SKUList = new List<ServiceItemSKU>();
			this.CreateTime = DateTime.Now;
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// 设置类别
		/// </summary>
		public void Categorize(ServiceCategory category)
		{
			this.Category = category;
		}

		/// <summary>
		/// 添加SKU属性
		/// </summary>
		public void AddSKUProperty(ServiceSKUProperty skuProperty)
		{
			if (this.SKUProperties == null)
			{
				throw new HangerdException("服务项: {0} 属性列表为null", this.Name);
			}

			var existProperty = this.SKUProperties.FirstOrDefault(skup => skup.Id == skuProperty.Id);

			if (existProperty == null)
			{
				this.SKUProperties.Add(skuProperty);
			}
		}

		/// <summary>
		/// 移除SKU属性
		/// </summary>
		public void RemoveSKUProperty(ServiceSKUProperty skuProperty)
		{
			if (this.SKUProperties == null)
			{
				throw new HangerdException("服务项: {0} 属性列表为null", this.Name);
			}

			var existProperty = this.SKUProperties.FirstOrDefault(skup => skup.Id == skuProperty.Id);

			if (existProperty != null)
			{
				this.SKUProperties.Remove(skuProperty);
			}
		}

		public void AddSKU(ServiceSKUPropertyValue[] skuPropertyValues)
		{
			this.SKUList.Add(new ServiceItemSKU(this, skuPropertyValues));
		}

		#endregion

		#region Private Methods

		#endregion
	}
}
